package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.*;
import com.mt.roll.core.dal.service.*;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.constants.PortStatusConstant;
import com.mt.roll.wms.mapper.AreaLocationVoMapper;
import com.mt.roll.wms.mapper.ProcessLocationVoMapper;
import com.mt.roll.wms.params.*;
import com.mt.roll.wms.service.ProcessLocationService;
import com.mt.roll.wms.service.StorageBoxCircInfoService;
import com.mt.roll.wms.utils.SnowflakeId;
import com.mt.roll.wms.utils.SnowflakeIdWorker;
import com.mt.roll.wms.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther yanyang
 * @date 2022/2/25 9:56
 */

@Service
@Transactional
public class ProcessLocationServiceImpl extends BaseService implements ProcessLocationService {

    @Autowired
    private ProcessLocationServiceBiz processLocationServiceBiz;

    @Autowired
    private ProcessLocationVoMapper processLocationVoMapper;

    @Autowired
    private AreaLocationVoMapper areaLocationVoMapper;

    @Autowired
    private ProcessLocationService processLocationService;

    @Autowired
    private StorageBoxCircInfoService storageBoxCircInfoService;

    @Override
    public R<PageVo<ProcessLocationVo>> page(ProcessLocationQueryParam processLocationQueryParam) {
        IPage<ProcessLocationVo> page = processLocationVoMapper.page(new Page<>(processLocationQueryParam.getCurrent(), processLocationQueryParam.getSize()), processLocationQueryParam);
        return successful(new PageVo<>(page, ProcessLocationVo.class));
    }

    @Override
    public R<ProcessLocationVo> getById(IdParam idParam) {
        ProcessLocation processLocation = processLocationServiceBiz.getById(idParam.getId());
        Assert.notNull("详情不存在", processLocation);
        ProcessLocationVo processLocationVo = processLocationVoMapper.getById(idParam.getId());
        return successful(processLocationVo);
    }

    @Override
    public R<IdVo> add(ProcessLocationParam processLocationParam) {
        //1. 绑定 工序-库位
        QueryWrapper<ProcessLocation> wrapper1 = new QueryWrapper<>();
        wrapper1.eq(ProcessLocation.WORK_SEQUEN_ID, processLocationParam.getWorkSequenId())
                .eq(ProcessLocation.LOCATION_ID, processLocationParam.getLocationId());
        Assert.eqZero(processLocationServiceBiz.count(wrapper1), "该工序库位已经绑定");


        QueryWrapper<ProcessLocation> wrapper2 = new QueryWrapper<>();
        wrapper2.eq(ProcessLocation.LOCATION_ID, processLocationParam.getLocationId());
        Assert.eqZero(processLocationServiceBiz.count(wrapper2), "该库位已被其他工序绑定");

        ProcessLocation processLocation = new ProcessLocation();
        BeanUtils.copyProperties(processLocationParam, processLocation);
        setCommonField(processLocation);
        processLocationServiceBiz.save(processLocation);

        //2.第二步 调用 service  绑定 库位-存储箱
        return successful(IdVo.builder().id(processLocation.getId()).build());
    }

    /*
    @Override
    public R<IdListVo> processlocation(ProcessLocationListParam processLocationListParam) {
        Long workSequenId = processLocationListParam.getWorkSequenId();
        for (ProcessLocationParam processLocationParam : processLocationListParam.getProcessLocationParam()) {
            processLocationParam.setWorkSequenId(workSequenId) ;
            //调用 service  绑定 工序-库位
            processLocationService.add(processLocationParam);

            Long locationId = processLocationParam.getLocationId();
            for (StorageBoxCircInfoParam storageBoxCircInfoParam:processLocationParam.getStorageBoxCircInfoParam()){
                storageBoxCircInfoParam.setCurrLocationId(locationId);
                //调用 service  绑定 库位-存储箱
                storageBoxCircInfoService.add(storageBoxCircInfoParam);
            }
        }
        return null;
    }*/

    @Override
    public R<IdVo> processlocation(ProcessLocationListParam processLocationListParam) {
        ProcessLocationParam processLocationParam = new ProcessLocationParam();
        StorageBoxCircInfoParam storageBoxCircInfoParam = new StorageBoxCircInfoParam();
        if (processLocationListParam!=null) {
            for (ProcessLocationStorageParam processLocationStorageParam : processLocationListParam.getProcessLocationStorageList()) {
                //1.绑定 工序-库位
                processLocationParam.setWorkSequenId(processLocationListParam.getWorkSequenId());
                processLocationParam.setLocationId(processLocationStorageParam.getLocationId());
                processLocationParam.setPortAttrId(processLocationStorageParam.getPortAttrId());
                processLocationService.add(processLocationParam);
                //2.绑定 库位-存储箱
                storageBoxCircInfoParam.setInProcessCode(processLocationListParam.getWorkSequenId().toString());
                storageBoxCircInfoParam.setCurrLocationId(processLocationStorageParam.getLocationId());
                storageBoxCircInfoParam.setStorageBoxId(processLocationStorageParam.getStorageId());
                storageBoxCircInfoParam.setLocationStorageSubId(SnowflakeId.getSnowFlakeId());
                storageBoxCircInfoService.add(storageBoxCircInfoParam);
            }
        }
        return successful(IdVo.builder().id(processLocationListParam.getId()).build());
    }

    @Override
    public R<IdVo> update(ProcessLocationParam processLocationParam) {
        ProcessLocation processLocation = processLocationServiceBiz.getById(processLocationParam.getId());
        /*
        if (!processLocation.getWorkSequenId().equals(processLocationParam.getWorkSequenId()) || !processLocation.getLocationId().equals(processLocationParam.getLocationId())) {
            QueryWrapper<ProcessLocation> wrapper = new QueryWrapper<>();
            wrapper.eq(ProcessLocation.WORK_SEQUEN_ID, processLocationParam.getWorkSequenId())
                    .eq(ProcessLocation.LOCATION_ID, processLocationParam.getLocationId());
            Assert.eqZero(processLocationServiceBiz.count(wrapper), "该工序库位已经绑定");
        }

        QueryWrapper<ProcessLocation> wrapper = new QueryWrapper<>();
        wrapper.eq(ProcessLocation.WORK_SEQUEN_ID, processLocationParam.getWorkSequenId())
                .eq(ProcessLocation.LOCATION_ID, processLocationParam.getLocationId());
        Assert.eqZero(processLocationServiceBiz.count(wrapper), "该工序库位已经绑定");
        */
        ProcessLocation updateAlarm = new ProcessLocation();
        BeanUtils.copyProperties(processLocationParam, updateAlarm);
        setUpdateCommonField(updateAlarm);
        processLocationServiceBiz.updateById(updateAlarm);
        return successful(IdVo.builder().id(processLocation.getId()).build());
    }

    @Override
    public R<IdVo> delete(IdParam idParam) {
        ProcessLocation processtLocation = processLocationServiceBiz.getById(idParam.getId());
        if (processtLocation == null) {
            return failed("信息不存在");
        }
        processLocationServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(processtLocation.getId()).build());
    }


    @Autowired
    private WorkSequenceServiceBiz workSequenceServiceBiz;

    @Override
    public R<List<WorkSequenceVo>> workSequenList(){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(WorkSequence.VALID,1);
        List<WorkSequence> workSequenceList = workSequenceServiceBiz.list(wrapper);
        List<WorkSequenceVo> workSequenceVoList = com.mt.roll.core.utils.BeanUtils.copyList(workSequenceList, WorkSequenceVo.class);
        return successful(workSequenceVoList);
    }

    @Autowired
    private LocationServiceBiz locationServiceBiz;

    @Override
    public R<List<LocationListVo>> locationList(){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(SysDictData.VALID,1);
        List<Location> locationList = locationServiceBiz.list(wrapper);
        List<LocationListVo> locationVoList = com.mt.roll.core.utils.BeanUtils.copyList(locationList, LocationListVo.class);
        return successful(locationVoList);
    }


    @Autowired
    private SysDictDataServiceBiz sysDictDataServiceBiz;

    @Override
    public R<List<SysDictDataVo>> portAttrList() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(SysDictData.TYPE_NAME, "库位属性");
        List<SysDictData> sysDictDataList = sysDictDataServiceBiz.list(wrapper);
        List<SysDictDataVo> sysDictDataVoList = com.mt.roll.core.utils.BeanUtils.copyList(sysDictDataList, SysDictDataVo.class);
        return successful(sysDictDataVoList);
    }

    @Override
    public R<PageVo<AreaLocationVo>> locationByProcess(ProcessQueryParam processQueryParam) {
        IPage<AreaLocationVo> page = areaLocationVoMapper.locationByProcess(new Page<>(processQueryParam.getCurrent(), processQueryParam.getSize()), processQueryParam);
        return successful(new PageVo<>(page, AreaLocationVo.class));
    }


}
